package org.example.supermarketms.config;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

@org.springframework.context.annotation.Configuration
public class HBaseConfig {

    @Value("${hbase.zk-quorum}")
    private String zkQuorum;

    @Bean
    public Connection hBaseConnection() {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum", zkQuorum);
        //或者通过引用配置文件
//        configuration.addResource("hbase-site.xml");
        try {
            return ConnectionFactory.createConnection(configuration);
        } catch (Exception e) {
            throw new RuntimeException("Failed to connect to HBase", e);
        }
    }

    @Bean
    public TableName tableName(@Value("${hbase.table-name}") String name) {
        return TableName.valueOf(name);
    }
}
